package com.wx.spring.utils;

import com.wx.spring.constant.BasicDataType;

/**
 * @author wx
 * @Description: 转换工具类
 * @date 2019/1/4 000417:57
 */
public class ConvertUtis {

    private ConvertUtis() {
        throw new RuntimeException("工具类禁止实例化");
    }

    /**
     * @param className 类名
     * @param parameter 属性
     * @description: 根据传入的属性和类名，将属性名强转为类名的属性
     * @author: wx
     * @date: 2019/1/4 0004
     * @return: java.lang.Object
     */
    public static Object convert(String className, String parameter) {
        if (BasicDataType.STRING.equals(className)) {
            return parameter;
        } else if (BasicDataType.INTEGER.equals(className) || BasicDataType.BASIC_INTEGER.equals(className)) {
            return Integer.valueOf(parameter);
        } else if (BasicDataType.FLOAT.equals(className) || BasicDataType.BASIC_FLOAT.equals(className)) {
            return Float.valueOf(parameter);
        } else if (BasicDataType.DOUBLE.equals(className) || BasicDataType.BASIC_DOUBLE.equals(className)) {
            return Double.valueOf(parameter);
        } else if (BasicDataType.LONG.equals(className) || BasicDataType.BASIC_LONG.equals(className)) {
            return Long.valueOf(parameter);
        } else if (BasicDataType.SHORT.equals(className) || BasicDataType.BASIC_SHORT.equals(className)) {
            return Short.valueOf(parameter);
        } else if (BasicDataType.BYTE.equals(className) || BasicDataType.BASIC_BYTE.equals(className)) {
            return Byte.valueOf(parameter);
        } else if (BasicDataType.BOOLEAN.equals(className) || BasicDataType.BASIC_BOOLEAN.equals(className)) {
            return Boolean.valueOf(parameter);
        }
        return null;

    }

    /**
     * @param propertyName
     * @description: 属性名转换为set方法名
     * @author: wx
     * @date: 2019/1/4 0004
     * @return: java.lang.String
     */
    public static String getSetMethodNameByField(String propertyName) {
        String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
        return methodName;
    }


    /**
     * @description: 首字母转小写
     * @author: wx
     * @date: 2019/1/14 0014
     * @param s
     * @return: java.lang.String
     */
    public static String toLowerCaseFirstOne(String s){
        if(Character.isLowerCase(s.charAt(0))) {
            return s;
        }else {
            return (new StringBuilder()).append(Character.toLowerCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }


    /**
     * @description: 首字母转大写
     * @author: wx
     * @date: 2019/1/14 0014
     * @param s
     * @return: java.lang.String
     */
    public static String toUpperCaseFirstOne(String s){
        if(Character.isUpperCase(s.charAt(0))) {
            return s;
        }
        else {
            return (new StringBuilder()).append(Character.toUpperCase(s.charAt(0))).append(s.substring(1)).toString();
        }
    }
}
